package com.bear.cmcc.NetTestHTTP.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.bear.cmcc.NetTestHTTP.NetworkTestApp;
import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.InfoSubBean;
import com.bear.cmcc.NetTestHTTP.util.InforSubUtil;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 信息订阅的九宫图适配器
 * @author meizhen.xu
 *
 */
public class ChannelsAdapter extends BaseAdapter {
	
	private Context context; 
	private List<InfoSubBean> channels;
	private int modeWidth;
	private int holdPosition;
	private boolean isChanged = false;
	private boolean ShowItem = false;
	
	public ChannelsAdapter(Context context){
		this.context = context;
		this.channels = new ArrayList<InfoSubBean>();
		this.modeWidth = MyCommonUtil.getWindowWidth(context)/2 - 20;
	}
	
	public void remove(int oldIndex){
		this.channels.remove(oldIndex);
	}
	
	public void add(int newIndex,InfoSubBean info){
		this.channels.add(newIndex, info);
	}

	@Override
	public int getCount() {
		return channels.size();
	}

	@Override
	public InfoSubBean getItem(int location) {
		return channels.get(location);
	}

	@Override
	public long getItemId(int position) {
		return Long.parseLong(getItem(position).getId());
	}
	
	public void addObject(InfoSubBean b){
		this.channels.add(b);
	}
	
	public void clear(){
		this.channels.clear();
	}
	
	
	public List<InfoSubBean> getChannels() {
		return channels;
	}

	public void exchange(int startPosition, int endPosition) {
		holdPosition = endPosition;
		InfoSubBean startObject = getItem(startPosition);
		if(startPosition < endPosition){
			channels.add(endPosition + 1, startObject);
		    channels.remove(startPosition);
		}else{
			channels.add(endPosition,startObject);
			channels.remove(startPosition + 1);
		}
		isChanged = true;
		notifyDataSetChanged();
	}
	
	public void showDropItem(boolean showItem){
		this.ShowItem = showItem;		
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		InfoSubBean b = getItem(position);
		if(b.getIsAddChannel()==1){
			view = LayoutInflater.from(context).inflate(R.layout.item_managerchannel, null);
			LinearLayout bgLayout = (LinearLayout)view.findViewById(R.id.itemManChannel_bgLayout);
			bgLayout.setMinimumWidth(modeWidth);
			bgLayout.setMinimumHeight(modeWidth);
			
		}else{
			view = LayoutInflater.from(context).inflate(
					R.layout.item_channels, null);

			LinearLayout bgLayout= (LinearLayout)view.findViewById(R.id.itemChannels_bgLayout);
			
			bgLayout.setMinimumWidth(modeWidth);
			bgLayout.setMinimumHeight(modeWidth);
			
			TextView tvTitle = (TextView) view.findViewById(R.id.itemChannels_titleText);
			ImageView iv=(ImageView)view.findViewById(R.id.itemChannels_image);
			TextView numberText = (TextView)view.findViewById(R.id.itemChannels_numberText);
			
			
			tvTitle.setText(b.getTitle());
			iv.setImageBitmap(InforSubUtil.getImageFromAssetsFile(b.getPath(), context));
			numberText.setText(""+b.getNumber());
			
		}
		
		if (isChanged){
		    if (position == holdPosition){
		    	if(!ShowItem){
			        view.setVisibility(View.INVISIBLE);
		    	}
		    }
		}
		return view;
		
	}

}
